#include <e.h>
#include "e_mod_main.h"

#include <stdlib.h>
#include <stdint.h>
#include <err.h>
#include <sys/types.h>
#include <sys/time.h>

struct _E_Config_Dialog_Data
{
    double bat_hysteresis;
    double bat_lf_low_temp;
    double bat_lf_high_temp;
    double bat_rf_low_temp;
    double bat_rf_high_temp;
    
    double ac_hysteresis;
    double ac_lf_low_temp;
    double ac_lf_high_temp;
    double ac_rf_low_temp;
    double ac_rf_high_temp;
};

/* Protos */
static void *_create_data (E_Config_Dialog * cfd);
static void _free_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata);
static Evas_Object *_basic_create_widgets (E_Config_Dialog * cfd, Evas * evas,
        E_Config_Dialog_Data * cfdata);
static int _basic_apply_data (E_Config_Dialog * cfd,
                              E_Config_Dialog_Data * cfdata);
static void _fill_data (Config_Item * ci, E_Config_Dialog_Data * cfdata);

/* Config Calls */
void
_config_ei8k_module (Config_Item * ci)
{
    E_Config_Dialog *cfd;
    E_Config_Dialog_View *v;
    E_Container *con;
    char buf[4096];

    if (!ei8k_config)
        return;

    v = E_NEW (E_Config_Dialog_View, 1);
    if (v == NULL)
    {
        warn ("malloc()");
        return;
    }

    v->create_cfdata = _create_data;
    v->free_cfdata = _free_data;
    v->basic.apply_cfdata = _basic_apply_data;
    v->basic.create_widgets = _basic_create_widgets;

    snprintf (buf, sizeof (buf), "%s/module.edj",
              e_module_dir_get (ei8k_config->module));
    con = e_container_current_get (e_manager_current_get ());
    cfd = e_config_dialog_new (con, D_ ("Ei8k Configuration"), "Ei8k", "_e_modules_ei8k_config_dialog", buf, 0, v, ci);
    ei8k_config->config_dialog = cfd;
}

static void
_fill_data (Config_Item * ci, E_Config_Dialog_Data * cfdata)
{
    if (!cfdata)
        return;
        
    cfdata->ac_hysteresis    = ci->ac_hysteresis;  
    cfdata->ac_lf_high_temp  = ci->ac_lf_high_temp;
    cfdata->ac_rf_high_temp  = ci->ac_rf_high_temp;
    cfdata->ac_lf_low_temp   = ci->ac_lf_low_temp;
    cfdata->ac_rf_low_temp   = ci->ac_rf_low_temp;
    cfdata->bat_hysteresis   = ci->bat_hysteresis; 
    cfdata->bat_lf_high_temp = ci->bat_lf_high_temp;
    cfdata->bat_rf_high_temp = ci->bat_rf_high_temp;
    cfdata->bat_lf_low_temp  = ci->bat_lf_low_temp;
    cfdata->bat_rf_low_temp  = ci->bat_rf_low_temp;
}

static void *
_create_data (E_Config_Dialog * cfd)
{
    E_Config_Dialog_Data *cfdata;
    Config_Item *ci;

    if (!cfd)
        return NULL;

    ci = cfd->data;
    cfdata = E_NEW (E_Config_Dialog_Data, 1);
    if (cfdata == NULL)
    {
        warn ("malloc()");
        return NULL;
    }

    _fill_data (ci, cfdata);

    return cfdata;
}

static void
_free_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata)
{
    if (ei8k_config == NULL)
        return;
    ei8k_config->config_dialog = NULL;

    free (cfdata);
    cfdata = NULL;
}

static Evas_Object *
_basic_create_widgets (E_Config_Dialog * cfd, Evas * evas,
                       E_Config_Dialog_Data * cfdata)
{
    Evas_Object *o, *of, *ob, *o_temps;

    o = e_widget_list_add (evas, 0, 0);
    o_temps = e_widget_table_add(evas, 1);

    of = e_widget_framelist_add (evas, D_ ("AC Temperatures"), 0);

    ob = e_widget_label_add (evas, D_ ("Hysteresis:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 1.0, 10.0,
                              1.0, 0, &(cfdata->ac_hysteresis), NULL, 100);
    e_widget_framelist_object_append (of, ob);


    ob = e_widget_label_add (evas, D_ ("Right Fan Low Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->ac_rf_low_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);


    ob = e_widget_label_add (evas, D_ ("Right Fan High Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->ac_rf_high_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);


    ob = e_widget_label_add (evas, D_ ("Left Fan Low Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->ac_lf_low_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);


    ob = e_widget_label_add (evas, D_ ("Left Fan High Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->ac_lf_high_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);

    e_widget_table_object_append(o_temps, of, 0, 0, 1, 1, 0, 0, 0, 0);




    of = e_widget_framelist_add (evas, D_ ("Batery Temperatures"), 0);

    ob = e_widget_label_add (evas, D_ ("Hysteresis:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 1.0, 10.0,
                              1.0, 0, &(cfdata->bat_hysteresis), NULL, 100);
    e_widget_framelist_object_append (of, ob);


    ob = e_widget_label_add (evas, D_ ("Right Fan Low Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->bat_rf_low_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);


    ob = e_widget_label_add (evas, D_ ("Right Fan High Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->bat_rf_high_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);
    

    ob = e_widget_label_add (evas, D_ ("Left Fan Low Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->bat_lf_low_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);


    ob = e_widget_label_add (evas, D_ ("Left Fan High Temp:"));
    e_widget_framelist_object_append (of, ob);

    ob = e_widget_slider_add (evas, 1, 0, _("%1.0f °C"), 20.0, 80.0,
                              1.0, 0, &(cfdata->bat_lf_high_temp), NULL, 100);
    e_widget_framelist_object_append (of, ob);

    e_widget_table_object_append(o_temps, of, 1, 0, 1, 1, 0, 0, 0, 0);


    e_widget_list_object_append (o, o_temps, 1, 1, 0.5);
    
    return o;
}

static int
_basic_apply_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata)
{
    Config_Item *ci;

    if (!cfd)
        return 0;
    ci = cfd->data;
    
    ci->ac_hysteresis    = (int)cfdata->ac_hysteresis;
    ci->ac_lf_high_temp  = (int)cfdata->ac_lf_high_temp;
    ci->ac_rf_high_temp  = (int)cfdata->ac_rf_high_temp;
    ci->ac_lf_low_temp   = (int)cfdata->ac_lf_low_temp;
    ci->ac_rf_low_temp   = (int)cfdata->ac_rf_low_temp;
    ci->bat_hysteresis   = (int)cfdata->bat_hysteresis;
    ci->bat_lf_high_temp = (int)cfdata->bat_lf_high_temp;
    ci->bat_rf_high_temp = (int)cfdata->bat_rf_high_temp;
    ci->bat_lf_low_temp  = (int)cfdata->bat_lf_low_temp;
    ci->bat_rf_low_temp  = (int)cfdata->bat_rf_low_temp;
    
    e_config_save_queue ();
    _ei8k_config_updated (ci->id);

    return 1;
}
